﻿Ext.define("CRM8000.Purchase.PWMainForm", {
    extend: "Ext.panel.Panel",

    border: 0,
    layout: "border",

    initComponent: function () {
        var me = this;

        Ext.define("CRM8000PWBill", {
            extend: "Ext.data.Model",
            fields: ["id", "ref", "billDate", "supplierName", "warehouse", "inputUserName",
                "businessUserName", "billStatus"]
        });
        var storePWBill = Ext.create("Ext.data.Store", {
            autoLoad: false,
            model: "CRM8000PWBill",
            data: []
        });

        var gridPWBill = Ext.create("Ext.grid.Panel", {
            columnLines: true,
            columns: [
                { header: "状态", dataIndex: "billStatus", menuDisabled: true, sortable: false, width: 80 },
                { header: "入库单号", dataIndex: "ref", width: 110, menuDisabled: true, sortable: false },
                { header: "业务日期", dataIndex: "billDate", menuDisabled: true, sortable: false },
                { header: "供应商", dataIndex: "supplierName", width: 200, menuDisabled: true, sortable: false },
                { header: "采购金额(元)", menuDisabled: true, sortable: false, align: "right" },
                { header: "入库仓库", dataIndex: "warehouse", menuDisabled: true, sortable: false },
                { header: "业务员", dataIndex: "businessUserName", menuDisabled: true, sortable: false },
                { header: "录单人", dataIndex: "inputUserName", menuDisabled: true, sortable: false }
            ],
            store: storePWBill
        });

        Ext.define("CRM8000PWBillDetail", {
            extend: "Ext.data.Model",
            fields: ["id", "ref", "billDate", "supplierName", "warehouse", "inputUserName",
                "businessUserName"]
        });
        var storePWBillDetail = Ext.create("Ext.data.Store", {
            autoLoad: false,
            model: "CRM8000PWBillDetail",
            data: []
        });

        var gridPWBillDetail = Ext.create("Ext.grid.Panel", {
            tbar: [
                {
                    text: "新增采购入库单商品明细", iconCls: "CRM8000-button-add-detail", scope: this, handler: this.onAddPWBillDetail
                }, "-",
                {
                    text: "编辑采购入库单商品明细", iconCls: "CRM8000-button-edit-detail", scope: this
                }, "-", {
                    text: "删除采购入库单商品明细", iconCls: "CRM8000-button-delete-detail", scope: this
                }
            ],
            columnLines: true,
            columns: [
                { header: "商品编号", dataIndex: "ref", menuDisabled: true, sortable: false },
                { header: "商品名称/规格型号", dataIndex: "billDate", width: 250, menuDisabled: true, sortable: false },
                { header: "采购数量", dataIndex: "supplierName", menuDisabled: true, sortable: false, align: "right" },
                { header: "单位", dataIndex: "warehouse", menuDisabled: true, sortable: false },
                { header: "采购单价", dataIndex: "businessUserName", menuDisabled: true, sortable: false, align: "right" },
                { header: "采购金额(元)", dataIndex: "inputUserName", menuDisabled: true, sortable: false, align: "right" }
            ],
            store: storePWBillDetail
        });

        Ext.apply(me, {
            tbar: [
                    {
                        text: "新建采购入库单主记录", iconCls: "CRM8000-button-add", scope: this, handler: this.onAddPWBill
                    }, "-", {
                        text: "编辑采购入库单主记录", iconCls: "CRM8000-button-edit", scope: this
                    }, "-", {
                        text: "删除采购入库单", iconCls: "CRM8000-button-delete", scope: this
                    }, "-", {
                        text: "提交入库", iconCls: "CRM8000-button-commit", scope: this
                    }, "-", {
                        text: "关闭", iconCls: "CRM8000-button-exit", handler: function () {
                            location.replace("/");
                        }
                    }
            ],
            items: [{
                region: "north", height: "30%",
                split: true, layout: "fit", border: 0,
                items: [gridPWBill]
            }, {
                region: "center", layout: "fit", border: 0,
                items: [gridPWBillDetail]
            }]
        });

        this.pwBillGrid = gridPWBill;
        this.pwBillDetailGrid = gridPWBillDetail;

        me.callParent(arguments);

        me.refreshPWBillGrid();
    },

    refreshPWBillGrid: function (id) {
        var grid = this.pwBillGrid;
        var el = grid.getEl() || Ext.getBody();
        el.mask(CRM8000.Const.LOADING);
        Ext.Ajax.request({
            url: "/Purchase/GetPWBillList",
            method: "POST",
            callback: function (options, success, response) {
                var store = grid.getStore();

                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);

                    for (var i = 0; i < data.length; i++) {
                        var item = data[i];
                        store.add({
                            id: item.id, ref: item.ref
                        });
                    }

                    if (store.getCount() > 0) {
                        if (id) {
                            var r = store.findExact("id", id);
                            if (r != -1) {
                                grid.getSelectionModel().select(r);
                            }
                        } else {
                            grid.getSelectionModel().select(0);
                        }
                    }
                }

                el.unmask();
            }
        });
    },

    onAddPWBill: function () {
        var form = Ext.create("CRM8000.Purchase.PWEditForm");
        form.show();
    },

    onAddPWBillDetail: function () {
        var form = Ext.create("CRM8000.Purchase.PWDetailEditForm");
        form.show();
    }
});